<?php
namespace app\push\controller;

// use \GatewayWorker\Lib\Gateway;
use \GatewayWorker\Lib\Gateway;

class Worker
{
    

    private function checkKey($data){
        return true;
    	$key = substr($data,0,32);
        // set_log('接收到的key：'.$key);
    	$end = substr($data,32);
        // set_log('截取的字符串：'.$end);
        // set_log('加密的字符串：'.$end .$this->key);
    	$com = md5($end .$this->key);
        // set_log('加密得到的key：'.$com);
    	if($key == $com){
    		return true;
    	}
    }

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage()
    {
        $client_id = $_REQUEST['client_id'];
        $message = $_REQUEST['message'];
        $data = substr($message,1,-1);
    	$bool = $this->checkKey($data);
        //dump($bool);
    	if($bool){//验证签名是否正确
    		$data = substr($data,32);
	    	$totle = explode(',', $data);
            $info = explode('*', $totle[0]);
	    	if(isset($info[3]) && $info[3] == 'KA'){
	    		//如果是链路保持，原样返回数据;
    			$this->_deviceStatus = new \app\common\model\DeviceStatus();
    			$this->_deviceStatus->addstatus($info);
    			 
	        	
		    }else if(isset($info[3]) && $info[3] == 'UD'){
	    		$this->_deviceStatus = new \app\common\model\DeviceMessage();
    			$this->_deviceStatus->addmessage($info['1'],$totle);
		    }

    	} 
        dump('ok');
    	
    }

    

}